# -*- coding: utf-8 -*-
# @Time    : 2020/11/22 12:32
# @Author  : chinablue
# @File    : dingtalk_helper.py

import requests
# 在钉钉上创建应用后获取

from application.settings import app_key,app_secret
dd_domain = "https://oapi.dingtalk.com"


class DingtalkHelper():
 
    def __init__(self):
        self.access_token = self.get_token()

    # 获取token--------------------------------------------------------------------------------------------------
    def get_token(self):
        url = f"{dd_domain}/gettoken"
        data = {
            "appkey": app_key,
            "appsecret": app_secret,
        }
        res_json = requests.get(url=url, params=data).json()
        return res_json.get("access_token")

    # 获取部门列表-----------------------------------------------------------------------------------------------
    def get_deptList(self):
        url = f"{dd_domain}/topapi/v2/department/listsub"
        data = {
            "access_token": self.access_token,
        }
        res_json = requests.post(url=url, params=data).json()
        return res_json.get('result')
    
    # 获取角色列表-----------------------------------------------------------------------------------------------
    def get_roleList(self):
        url = f"{dd_domain}/topapi/role/list"
        data = {
            "access_token": self.access_token,
        }
        res_json = requests.post(url=url, params=data).json()
        return res_json.get('result')
    
    # 获取部门下员工列表-----------------------------------------------------------------------------------------------
    def get_usersListByDept(self,deptId):
        url = f"{dd_domain}/topapi/v2/user/list"
        data = {
            "access_token": self.access_token,
            "dept_id":deptId,
            "cursor":0,
            "size":100 
        }
        res_json = requests.post(url=url, params=data).json()
        return res_json.get('result')

    # 获取部员工详细信息-----------------------------------------------------------------------------------------------
    def get_usersInfoByUserId(self,UserId):
        url = f"{dd_domain}/topapi/v2/user/get"
        data = {
            "access_token": self.access_token,
            "userid":UserId
        }
        res_json = requests.post(url=url, params=data).json()
        return res_json.get('result')

